/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin;

import connect.ConnectDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Thuan
 */
public class CheckValidity {
    private ConnectDatabase ketnoi;
    private Connection con;
    private Pattern pattern1;
    private Matcher matcher1;
    private Pattern pattern2;
    private Matcher matcher2;
    private Pattern pattern3;
    private Matcher matcher3;
    private static final String MA_THE = "^13690\\d{10}$";
    
    private static final String NGAY_SINH = "^\\d{1,2}\\/\\d{1,2}\\/\\d{1,4}$";
    private static final String USER_NAME = "^[a-zA-Z0-9_.]+$";
    public Boolean ktTonTai(String table,String column,String dk ){
        ketnoi = new ConnectDatabase();
        con = ketnoi.connect();
        Statement stm;
        boolean kq = false;
        try {
            stm = con.createStatement();
            ResultSet rs = stm.executeQuery("Select * from "+ table +" where "+ column +" = '"+ dk +"'");
            if(rs.next()){
                if(rs.getString(column).equals(dk)){
                    kq = true;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(AddUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
        
    }

    public Boolean ktDangNhap(String user, String pass){
    boolean kq = false;
        ketnoi = new ConnectDatabase();
        con = ketnoi.connect();
        Statement stm;
        try {
            stm = con.createStatement();
            ResultSet rs = stm.executeQuery("Select * from Admin where TenDangNhap = '" + user + "' and MatKhau ='" + pass + "'");
            if(rs.next()){
                if(rs.getString("TenDangNhap").equals(user)){
                    if(rs.getString("MatKhau").equals(pass)){
                        kq = true;
                    }
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(CheckValidity.class.getName()).log(Level.SEVERE, null, ex);
        }
    return kq;
    }
    public CheckValidity() {
        pattern1 = Pattern.compile(MA_THE);
        pattern2 = Pattern.compile(NGAY_SINH);
        pattern3 = Pattern.compile(USER_NAME);
        
    }
    public boolean ktMaThe(final String mathe){
        matcher1 = pattern1.matcher(mathe);
        return matcher1.matches();
    }
    public boolean ktNgaySinh(final String ngaysinh){
        matcher2 = pattern2.matcher(ngaysinh);
        return matcher2.matches();
    }
    public boolean ktUserName(final String user){
        matcher3 = pattern3.matcher(user);
        return matcher3.matches();
    }
    
}
